<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-left">
        <img src="@/assets/logo.png" alt="logo" class="login-logo" />
        <h1>{{ title }}</h1>
      </div>
      <div class="login-right">
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入邮箱或用户名" size="large" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password size="large" />
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" type="primary" @click="handleLogin" style="width: 100%;"
              size="large">登录</el-button>
          </el-form-item>
        </el-form>
        <!-- <div class="login-footer">
          <a href="#">忘记密码？</a>
          <a href="#">注册</a>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { getInfo, loginWithoutSSO } from '@/api/base/token'
import { getPermissionsByUserId } from '@/api/base/permissions'
import { useUserStore } from '@/store/user'
const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名或邮箱', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = () => {
  loginFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      try {
        loading.value = true
        ElMessage.warning('登录中...')
        const res = await loginWithoutSSO(loginForm.username, loginForm.password)
        localStorage.setItem("token", res.access_token);
        localStorage.setItem("refresh_token", res.refresh_token);
        const userInfo = await getInfo();
        const permissions = await getPermissionsByUserId(userInfo.id);
        userStore.addUserInfo(userInfo, permissions)
        // 模拟登录成功
        ElMessage.success('登录成功')
        loading.value = false
        router.replace('/')
      } catch (error) {
        loading.value = false
      } finally {
        loading.value = false
      }

    }
  })
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f6f8fa;
}

.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #fff;
  padding: 2rem;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(27, 31, 35, 0.12), 0 8px 24px rgba(27, 31, 35, 0.12);
  width: 320px;
}

.login-logo {
  width: 48px;
  height: 48px;
  margin-bottom: 1rem;
}

.login-left h1 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 1.5rem;
  text-align: center;
  width: 100%;
}

.login-footer {
  margin-top: 1rem;
  text-align: right;
}

.login-footer a {
  font-size: 12px;
  color: #0969da;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}
</style>